Soru & Cevap

Json EPDK'dan XML verisi cekmek

03.04.2016 - 02:32

Arkadaşlar Merhaba.

EPDK'dan güncel petrol fiyatlarını Android Studio kullanarak çekmek istiyorum.

https://dbs.epdk.org.tr/EPDKPetrolWS/EPDKPetrol.asmx?op=PetrolPiyasasiIllereGoreAkaryakitFiyatlari

Epdk'nın bu adreste bulunan XML'ini kullanarak verileri çekmeye çalıştım. il kodunu girip (Örnek:15) butona basınca hata vermiyor ama program tepki de vermiyor. Kod parçacığını paylaşıyorum. Yardımlarınızı bekliyorum.

MAIN ACTIVITY JAVA CLASS'IM

package com.example.wesley.jsonornek;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.io.IOException;


public class MainActivity extends Activity {

    private static final String SOAP_ACTION= "http://dbs.epdk.org.tr/EPDKPetrolWS/PetrolPiyasasiIllereGoreAkaryakitFiyatlari";
    private static final String METHOD_NAME= "PetrolPiyasasiIllereGoreAkaryakitFiyatlari";
    private static final String NAME_SPACE= "http://dbs.epdk.org.tr";
    private static final String URL= "http://www.w3.org/2001/XMLSchema-instance";

    EditText il_kodu;
    TextView tarih, yakit, il, firma, fiyat;
    Button sorgula;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        il_kodu = (EditText)findViewById(R.id.editText);
        tarih = (TextView)findViewById(R.id.textView);
        yakit = (TextView)findViewById(R.id.textView2);
        il = (TextView)findViewById(R.id.textView3);
        firma = (TextView)findViewById(R.id.textView4);
        fiyat = (TextView)findViewById(R.id.textView5);
        sorgula = (Button) findViewById(R.id.button);

        sorgula.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SoapObject request = new SoapObject(NAME_SPACE,METHOD_NAME);
                request.addProperty("Petrol", il_kodu.getText().toString());

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.debug = true;

                try {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

                    tarih.setText(response.toString());


                }
                catch(Exception e1){

                e1.printStackTrace();
                }


            }
        });




    }
}

 ACTIVITY_MAIN.XML DOSYAM

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wesley.jsonornek.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:hint="il kodu giriniz" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sorgula"
        android:id="@+id/button"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tarih"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Yakıt Tipi"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="İl"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Firma"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fiyat"
        android:id="@+id/textView5"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

INTERNET İZNİ VERMEK İÇİN GEREKLİ MANIFEST DOSYAM

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wesley.jsonornek">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:permission="INTERNET"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

BU DA PROGRAMI GENYMOTION İLE ÇALIŞTIRDIĞIMDA ALDIĞIM MAVİ HATA ( MAVİ RENKLİ HATALARIN İSMİNİ BİLMİYORUM)

04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err: android.os.NetworkOnMainThreadException
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at com.example.wesley.jsonornek.MainActivity$1.onClick(MainActivity.java:59)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.view.View.performClick(View.java:4084)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.view.View$PerformClick.run(View.java:16966)
04-03 11:30:50.204 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.Handler.handleCallback(Handler.java:615)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.Looper.loop(Looper.java:137)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 11:30:50.208 5014-5014/com.example.wesley.jsonornek W/System.err:     at dalvik.system.NativeStart.main(Native Method)
04-03 11:30:53.632 5014-5014/com.example.wesley.jsonornek W/System.err: android.os.NetworkOnMainThreadException
04-03 11:30:53.632 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at com.example.wesley.jsonornek.MainActivity$1.onClick(MainActivity.java:59)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.view.View.performClick(View.java:4084)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.view.View$PerformClick.run(View.java:16966)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.Handler.handleCallback(Handler.java:615)
04-03 11:30:53.636 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 11:30:53.640 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.os.Looper.loop(Looper.java:137)
04-03 11:30:53.640 5014-5014/com.example.wesley.jsonornek W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 11:30:53.640 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 11:30:53.640 5014-5014/com.example.wesley.jsonornek W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
04-03 11:30:53.640 5014-5014/com.example.wesley.jsonornek W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 11:30:53.640 5014-5014/com.example.wesley.jsonornek W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 11:30:53.640 5014-5014/com.example.wesley.jsonornek W/System.err:     at dalvik.system.NativeStart.main(Native Method)

 

203 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-1372-1408467635.jpg
ahmtbrk
10.05.2016 - 11:53

Network işlemi gibi yorucu işlemleri UI Thread içerisinde yapmak sakıncalıdır politika gereği StrictMode hatası alıyorsunuz.
AsycnTask ya da thread kullanabilirsiniz.
https://gelecegiyazanlar.turkcell.com.tr/konu/android/egitim/android-301/asynctask

Bu kaynak işinizi görecektir.